/*
 * 文 件 名:  UserServiceImpl.java
 * 版    权:  Ysten Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  bo.chen
 * 修改时间:  2015年1月23日
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package lv.pig.base.jpa.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.PostConstruct;

import lv.pig.base.jpa.dao.UserDao;
import lv.pig.base.jpa.entity.User;
import lv.pig.base.jpa.service.UserService;
import lv.pig.base.utils.CommonUtils;

import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springside.modules.persistence.SearchFilter.Operator;

/**
 * <一句话功能简述> <功能详细描述>
 * 
 * @author bo.chen
 * @version [版本号, 2015年1月23日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service("userService")
public class UserServiceImpl implements UserService
{
    @Autowired
    UserDao userDao;
    
    @Autowired
    CredentialsMatcher credentialsMatcher;
    
    /**
     * @param entity
     * @return
     */
    @Override
    public User save(User entity)
    {
        return userDao.save(entity);
    }
    
    /**
     * @param userName
     * @return
     */
    @Override
    public User findByUserName(String userName)
    {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(CommonUtils.build(Operator.EQ, "userName"), userName);
        return this.find(param);
    }
    
    /**
     * @param loginName
     * @return
     */
    @Override
    public User findByLoginName(String loginName)
    {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(CommonUtils.build(Operator.EQ, "loginName"), loginName);
        User entity = this.find(param);
        if (null == entity)
        {
            // 用户名查询
            param = new HashMap<String, Object>();
            param.put(CommonUtils.build(Operator.EQ, "name"), loginName);
            entity = this.find(param);
        }
        
        if (null == entity)
        {
            // 邮箱查询
            param = new HashMap<String, Object>();
            param.put(CommonUtils.build(Operator.EQ, "email"), loginName);
            entity = this.find(param);
        }
        
        if (null == entity)
        {
            // 手机查询
            param = new HashMap<String, Object>();
            param.put(CommonUtils.build(Operator.EQ, "phone"), loginName);
            entity = this.find(param);
        }
        return entity;
    }
    
    /**
     * @param param
     * @return
     */
    @Override
    public List<User> findAll(Map<String, Object> param)
    {
        Specification<User> spec = CommonUtils.buildSpecification(param, User.class);
        return userDao.findAll(spec);
    }
    
    /**
     * @param param
     * @param sorts
     * @param page
     * @return
     */
    @Override
    public Page<User> findAll(Map<String, Object> param, Pageable page)
    {
        Specification<User> spec = CommonUtils.buildSpecification(param, User.class);
        return userDao.findAll(spec, page);
    }
    
    @Override
    public User find(Map<String, Object> param)
    {
        Specification<User> spec = CommonUtils.buildSpecification(param, User.class);
        return userDao.findOne(spec);
    }
    
    @Override
    public User findById(Long userId)
    {
        return userDao.findOne(userId);
    }
    
    public User generatePassword(User user)
    {
        String salt = UUID.randomUUID().toString();
        user.setSalt(salt);
        String password = new Sha512Hash(user.getPassword(), user.getName() + salt, 99).toString();
        user.setPassword(password);
        return user;
    }
    
//    @PostConstruct
//    public void init()
//    {
//        User entity = new User();
//        entity.setPassword("123456");
//        entity.setLoginName("admin");
//        entity.setName("admin123");
//        entity.setPhone("18552012652");
//        entity.setEmail("year0123@qq.com");
//        this.generatePassword(entity);
//        this.save(entity);
//    }
}
